﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace DvtCap
{
    public enum PixelFormatEnum
    {
        PIXELFORMAT_UNKNOWN,
        PIXELFORMAT_BGRA8888 = 377888772,
        PIXELFORMAT_BGR888 = 374740996,
        PIXELFORMAT_RGB888 = 370546692,
        PIXELFORMAT_RGBA8888 = 373694468,
        PIXELFORMAT_ARGB8888 = 372645892,
        PIXELFORMAT_ABGR8888 = 376840196,
        PIXELFORMAT_NV12 = 842094158,
        PIXELFORMAT_IYUV = 1448433993,
        PIXELFORMAT_YUY2 = 844715353
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct Rect
    {
        public int x, y;
        public int w, h;
    };
    public unsafe class RendererWrapper
    {
        [DllImport(DvtCamWrapper.LibraryName, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        private unsafe static extern void* CreateRenderer(void* windows);
        [DllImport(DvtCamWrapper.LibraryName, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        private unsafe static extern void DestroyRenderer(void* handle);
        [DllImport(DvtCamWrapper.LibraryName, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        private unsafe static extern void DrawImage(void* handle, void* data,
            int width, int height, int stride, int format,
            void* src_rect = null, void* dst_rect = null);

        [DllImport(DvtCamWrapper.LibraryName, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        private unsafe static extern void DrawImageYUV(void* handle, void* Y, void* U, void* V,
            int width, int height, int Ystride, int Ustride, int Vstride, int format,
            void* src_rect = null, void* dst_rect = null);
        [DllImport(DvtCamWrapper.LibraryName, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        private unsafe static extern void Present(void* handle);

        int _format;
        unsafe void* handle = null;

        public RendererWrapper(IntPtr windowhandle)
        {
            handle = CreateRenderer(windowhandle.ToPointer());
        }
        ~RendererWrapper()
        {
            DestroyRenderer(handle);
        }

        public void init(PixelFormatEnum pixelformat, int x_offset = 0, int y_offset = 0)
        {
            _format = (int)pixelformat;
        }

        public bool render(void* buf, int stride, int width, int height)
        {
            if (handle != null)
            {
                DrawImage(handle, buf, width, height, stride, _format, null, null);
                return true;
            }
            return false;
        }

        public bool renderIYUV(void* Y, void* U, void* V, int Ystride, int Ustride, int Vstride, int width, int height)
        {
            if (handle != null)
            {
                DrawImageYUV(handle, Y, U, V, width, height, Ystride, Ustride, Vstride, _format, null, null);
                return true;
            }
            return false;
        }

        public bool Present()
        {
            if (handle != null)
            {
                Present(handle);
                return true;
            }
            return false;
        }

    }
}
